package com.xiangyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangyang.annotations.OperationLog;
import com.xiangyang.entity.Sceduling;
import com.xiangyang.entity.dto.Result;
import com.xiangyang.entity.vo.ScedulingAndEmployee;
import com.xiangyang.entity.vo.ScedulingLike;
import com.xiangyang.mapper.IScedulingMapper;
import com.xiangyang.service.IScedulingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ScedulingServiceImpl extends ServiceImpl<IScedulingMapper, Sceduling> implements IScedulingService {
    @Autowired
    private IScedulingMapper scedulingMapper;
@OperationLog(title = "排班管理", businessType = "添加排班")
    @Override
    public Result addSceduling(Sceduling sceduling) {
    if(sceduling.getDuty()==null||sceduling.getDoctorId()==null||sceduling.getDepartmentId()==null){
        return Result.error("输入有误，请重新输入");
      }
      return scedulingMapper.insert(sceduling)>0?Result.ok("新增成功"):Result.error("新增失败");

    }
@OperationLog(title = "排班管理", businessType = "删除排班",oldDataMethod = "getScedulingList")
    @Override
    public Result deleteSceduling(Long schedulingId) {
        if(schedulingId==null){
            return Result.error("输入有误");
        }
        int i = scedulingMapper.deleteById(schedulingId);
        if (i!=1)  {
            return Result.error("删除失败");
        }
        scedulingMapper.deleteById(schedulingId);  //删除后返回删除的行数
        return Result.ok(schedulingId);  //返回删除的id
    }

    @Override
    public Result getScedulingList() {
         return Result.ok(scedulingMapper.selectList(null));

    }
@OperationLog(title = "排班管理", businessType = "修改排班",oldDataMethod = "getScedulingList" ,newDataMethod = "getScedulingList")
    @Override
    public Result updataSceduling(Sceduling sceduling) {
        int i = scedulingMapper.updateById(sceduling);
        if(i!=1){
            return Result.ok("修改错误");
        }
        return Result.ok(sceduling);
    }

    @Override
    public Result getScedulingList(ScedulingLike scedulingLike) {
        Page<Sceduling> page = new Page<>(scedulingLike.getPage(), scedulingLike.getSize());
        LambdaQueryWrapper<Sceduling> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Sceduling::getDuty,scedulingLike.getDuty());
       return Result.ok(scedulingMapper.selectPage(page,queryWrapper));
    }


}
